<div id="result">
<?php echo $this->Session->flash(); ?>
<div class="div_header1"><?php if($this->data['brand_type']=="style") echo _('Styles'); elseif($this->data['brand_type']=="condition") echo _('Conditions'); else echo _('Brands'); ?> for 
<?php if(count($this->data['Categorybrands'])>0){ echo $this->data['Categorybrands'][0]['Category']['category_name'];}?>

</div>
<form method="post" id="global_brand"  name="global_brand" action="<?php echo Configure::read('base_url')."categorybrands/add/".$this->data['brand_type'];?>">
<br style="clear:both;">
<table width="100%" cellspacing="0" id="brandstable">
<?php 
if($this->data['Categorybrands']):
$i=0;
foreach ($this->data['Categorybrands'] as $categorybrand):
	$class = null;
	if ($i++ % 2 == 0)
	$class = ' class="alt"';
	
	?> 
  <tr>
	<td width="88" <?php echo $class;?>><?php echo $i; ?></td>
	<td width="92" <?php echo $class;?>><span id="inputbox_<?php echo $categorybrand['Categorybrand']['id'];?>"><?php echo $categorybrand['Brand']['brand_description'];?></span></td>
	<td width="90" <?php echo $class;?>><span id="savebutton_<?php echo $categorybrand['Categorybrand']['id'];?>">
	<a href="javascript:void(0);" class="clickToEdit" title="Click here to edit <?php echo $categorybrand['Brand']['brand_description'];?>" entryID="<?php echo $categorybrand['Categorybrand']['id'];?>" brandID="<?php echo $categorybrand['Categorybrand']['brand_id'];?>">Edit</a>
	 |		
	<a href="javascript:void(0);" onclick="return deleteconfirm(this);" controller="categorybrands" action="delete<?php echo $this->data['brand_type'];?>" title="Delete" ElmID="<?php echo $categorybrand['Categorybrand']['id'];?>" ElmID2="<?php echo $categorybrand['Categorybrand']['category_id'];?>">Delete</a>
	</span>
	<span id="showsave_<?php echo $categorybrand['Categorybrand']['id']; ?>"></span>
	</td>
  </tr>
  <?php endforeach;else:?>
  <tr>
	<td class="alt"><b>No <?php echo $this->data['brand_type'];?>s found for this category. Use the following input box to create <?php echo $this->data['brand_type'];?>s for this category.</b></td>
  </tr>
  <?php endif;?>
  
</table> 
    
		
<table width="100%" id="add_global_form" border="0" cellspacing="0" cellpadding="0">
  <tr>
	<td class="alt" width="144">					
		<input type="hidden" name="data[Categorybrand][category_id]" value="<?php echo $this->data['cat_id']; ?>">
		<input type="hidden" name="data[Brand][brand_type]" value="<?php echo $this->data['brand_type']; ?>">
		<input type="hidden" name="data[Brand][global]" value="No">
		<?php //echo $this->Form->input('brand_description',array('label'=>false,'size'=>30,'id'=>'description','class'=>"required", 'title'=>"Enter ".$this->data['brand_type']."s name here")); ?>
		<!--input type="index" id='description' name="data[Brand][brand_description]" size="30" value="Enter <?php echo $this->data['brand_type']; ?> here" class="required" title="Enter <?php echo $this->data['brand_type']; ?> here" onblur="if(this.value=='') this.value='Enter <?php echo $this->data['brand_type']; ?> here'" onfocus="if(this.value=='Enter <?php echo $this->data['brand_type']; ?> here') this.value='';"/-->
		<input type="index" id='description' name="data[Brand][brand_description]" size="30" value="" class="required" />
		
	</td>
	<td width="347" class="alt" valign="top">
		<input type="submit" style="background:#73AE0B; color:#fff; font-size:14px;" name="Save" id="Save" value="Save" />
		&nbsp;
		<a onclick="showPop(this)" ElmID="<?php echo $this->data['cat_id']; ?>" ElmID2="<?php echo $this->data['brand_type']; ?>" title="Edit" action="addExisting" controller="add_category" href="javascript:void(0);">Add existing</a>
	</td>
	
  </tr>
</table>
       
<div class="div_bottom1"></div>
</form>
</div>
<?php echo $html->script(array("jquery.min.js","jquery.form.js","jquery.validate.js","jquery.pagination.js","jquery.tablesorter.min.js","jquery.tablednd.js")); ?>	 
 <script type="text/javascript">  
 jQuery.noConflict();

 jQuery(document).ready(function(){
 jQuery("#category_table").tablesorter({headers: { 2:{sorter: false},3:{sorter: false}, 4:{sorter: false}, 5:{sorter: false}}}); 
 
		var v = jQuery("#global_brand").validate({
			submitHandler: function(form) {
				jQuery(form).ajaxSubmit({
					target: "#result"
				});
			}
		});
		var v2 = jQuery("#brandsettings").validate({					
			submitHandler: function(form) {
			jQuery("#loader").show().fadeIn("slow");
				jQuery(form).ajaxSubmit({
					target: "#result"
				});
			}
		});
		
		
	jQuery("form#global_brand a.clickToEdit").click(function()				
		{
			if(jQuery(this).attr('class')=='clickToEdit'){
				var entryID=(jQuery(this).attr("entryID"));			
				var brandID=(jQuery(this).attr("brandID"));			
				var inputValue=jQuery("#inputbox_"+entryID).html();	
				
				var inputbox='<input type="hidden" value="'+brandID+'" name="data[id]"><input type="text" id="editbox_'+entryID+'" value="'+inputValue+'" name="data[brand_description]"><input type="hidden" value="'+inputValue+'" name="old_description" />';
				jQuery("#inputbox_"+entryID).html(inputbox);
				
				var savebutton='<a href="javascript:void(0);" entryID="'+entryID+'" title="Save" onclick="return saveEntry(this);">Save</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="javascript:void(0);" entryID="'+entryID+'" onclick="cancelForm(this)">Cancel</a>';
				jQuery("span#savebutton_"+entryID).hide().fadeOut("slow");		
				jQuery("span#showsave_"+entryID).live("click", cancelForm).html(savebutton);	
								
				jQuery('.required').attr('disabled',true);
				jQuery('#Save').attr('disabled',true);
				
				jQuery("form#global_brand a.clickToEdit").each(function (index,domEle) {
					  jQuery(domEle).removeClass("clickToEdit"); 
					  jQuery(domEle).addClass("clickToCancel"); 
				  });
			}
			else{
				return false;
			}			
			
		});
		
	});
	
	//Global brands/Styles/Conditions edit functions
	function saveEntry(param){		
		entryID=jQuery(param).attr("entryID");
		boxID="editbox_"+entryID;
		data = jQuery("#global_brand").serialize();
		inputValue=jQuery('input[id="'+boxID+'"]').val();
		jQuery('input[name="old_description"]').val(inputValue);	
		//alert(data);
		jQuery.ajax({
			type: 'POST',
			url: baseurl+"categorybrands/edit/",
			data: data,
			dataType: 'json',
			success: function(data){
				if(data.message=="No")
				return false;
			}
		});
		
		cancelForm(param);	
		return false;		
	}	
	
	function cancelForm(param){		
		var entryID=jQuery(param).attr("entryID");
		var boxID="editbox_"+entryID;
		var inputValue=jQuery('input[name="old_description"]').val();	
		jQuery("#inputbox_"+entryID).html(inputValue);
		jQuery("span#savebutton_"+entryID).show().fadeIn("slow");		
		jQuery("span#savebutton_"+entryID+" a").attr("class","clickToEdit");		
		jQuery("span#showsave_"+entryID).die("click", cancelForm).text("");
		
		jQuery("form#global_brand a.clickToCancel").each(function (index,domEle) {
					 jQuery(domEle).removeClass("clickToCancel"); 
					  jQuery(domEle).addClass("clickToEdit");
				  });
				  
		jQuery('.required').removeAttr('disabled');
		jQuery('#Save').removeAttr('disabled');
	}
	
	
	function deleteconfirm(param){
		if(confirm("Are you sure you want to delete it?"))
		showPop(param);
		else
		return false;
	}

        var i=1;
        var trs=jQuery("#brandstable tr");
        var length=trs.length;

        function moreItem(){
            trs.show();
            trs.each(function(index){
                if(index > i*14 && index < length){
                    jQuery(this).hide();
                }

            });
            i++;
            menuHide(parseInt(jQuery("#brandstable tr:hidden").length));
        }
        function lessItem(){
            i=i-2;
            moreItem();
            menuHide(parseInt(jQuery("#brandstable tr:hidden").length));
        }

        function menuHide(hiddenFields){
            diff=parseInt(length)-hiddenFields;
            if(diff<=15){
                jQuery(".less").hide();
                jQuery(".more").show();
                jQuery(".pipe").hide();
            }

            if(hiddenFields==0){
                jQuery(".less").show();
                jQuery(".more").hide();
                jQuery(".pipe").hide();
            }

            if(diff > 15 && hiddenFields > 0){
                jQuery(".less").show();
                jQuery(".more").show();
                jQuery(".pipe").show();
            }
        }

        //var elem=jQuery('<tr><td></td><td style="text-align: right;"><a onclick="lessItem()" class="less"> << Less</a>  <a onclick="moreItem()" class="more">More >></a></td></tr>').insertBefore(sel);
        
        if(length >= 15){
            jQuery("#brandstable").append('<tr><td></td><td></td><td style="text-align: right;"><a onclick="lessItem()" class="less"> << Less</a><span class="pipe"> || </span><a onclick="moreItem()" class="more">More >></a></td></tr>');
        }
        
        if(jQuery("#brandstable tr:first td").length > 1){
            moreItem();
        }
        
 </script>
<style type="text/css">
    .more, .less{
        cursor: pointer;
}
</style>
 <?php echo $this->element('sql_dump'); ?>